//============================================================================
// Name        : ClientConnection.h
// Author      : Allen Lee and Johnathan Ellithorpe
// Version     :
// Copyright   : UC Berkeley
// Description : 
//============================================================================
#include <string.h>
#include <string>
#include <sys/time.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include "Packet.h"

#ifndef CLIENTCONNECTION_H_
#define CLIENTCONNECTION_H_

class ClientConnection
{
private:
	int _fdSend;
	int _fdRecv;
		
	unsigned int _maxAcked;
	unsigned int _maxSent;

	unsigned int _timeoutCount;
	unsigned int _waitTime;
	unsigned int _slowStartThresh;
	unsigned int _numDups;
	unsigned int _CWND;
	unsigned int _offeredWindow;
	
	struct sockaddr_in _dstAddr;
	struct sockaddr_in _srcAddr;
	
	unsigned short _srcPort;
	unsigned short _dstPort;

	double _RTO;
	
	double _estRTT;
	double _deviation;
	
	struct timezone _tz;

public:
	ClientConnection();
	ClientConnection(char *host, unsigned int length, unsigned short port);
	virtual ~ClientConnection();
	
	int connect();
	int bindToAvailablePort();
	int sendPacket(Packet p);
	int packetizeAndSend(char *buf, unsigned int beginSeqNo, unsigned int numBytes);
	int send(char *buf, unsigned int numBytes);
	int closeConnection();
	int sendAndWaitForACK(int type);
	unsigned int max(unsigned int x, unsigned int y);
	unsigned int minInt(unsigned int x, unsigned int y);
	unsigned int minInt(unsigned int x, unsigned int y, unsigned int z);
	double min(double x, double y);
	double abs(double x);
	void closeSockets();
	int sendFile(int fd);
	int timeDiff(struct timeval *startTime, struct timeval *endTime);
};

#endif /*CLIENTCONNECTION_H_*/
